import os
import sys
import png2obj.image
import png2obj.stardrive.hull
import lxml.etree as ET

if len(sys.argv) < 2:
    print('Takes an XML filename as an argument.')
else:
    filename = sys.argv[1]
    
    head, tail = os.path.split(filename)
    
    tree = ET.parse(filename)
    shipData = tree.getroot()
    name = shipData.findtext("Name")
    
    if len(sys.argv) >= 3:
        outfile = sys.argv[2]
    else:
        outfile = os.path.join(head, name + '.png')

    slots = png2obj.stardrive.hull.shipData2Slots(shipData)
    numSlots = png2obj.stardrive.hull.countSlotRestrictionSets(slots)
    print("Slot counts: " + str(numSlots)) # empty, I, O, IO, E, IE, OE, IOE
    numAsy = png2obj.stardrive.hull.countAsymmetricalSlotPairs(slots)
    print('%s: %d asymmetrical slot pair(s)' % (name, numAsy))
    imageData = png2obj.stardrive.hull.slots2ImageData(slots, ("O", "I", "E"))
    png2obj.image.saveImage(outfile, imageData)
